package com.mr.sell.controller;

import com.mr.sell.constant.Constant;
import com.mr.sell.entity.SellerInfo;
import com.mr.sell.service.SellerInfoService;
import com.mr.sell.utils.ConstantEnum;
import com.mr.sell.utils.CookieUtil;
import com.mr.sell.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Class: SellerInfoController
 * User (作者):MRui
 * TODO (描述)：卖家用户
 * Date 2018.10.03 21:34
 */
@RestController
@Slf4j
@RequestMapping("/seller/info")
public class SellerInfoController {

    @Autowired
    private SellerInfoService sellerInfoService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/login")
    public ModelAndView login(@RequestParam("openid") String openid,
                              HttpServletResponse response,
                              Map<String,Object> map){

        //1.openid去和数据库里的数据匹配
        SellerInfo sellerInfo = sellerInfoService.findSellerInfoByOpenid(openid);
        if (sellerInfo == null){
            map.put("msg", ConstantEnum.LOGIN_FAIL.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error");
        }
        //2.设置token至redis
        String token = UUIDUtil.getUUID();
        int expire = Constant.REDIS_EXPIRE;//过期时间
        //key,value,过期时间，时间单位
        redisTemplate.opsForValue().set(String.format(Constant.REDIS_TOKEN_PREFIX,token),openid,expire, TimeUnit.SECONDS);
        //3.设置token至cookie
        CookieUtil.set(response,Constant.COOKIE_TOKEN,token,expire);


        return new ModelAndView("redirect:http:192.168.56.1:8080/sell/seller/order/list");
    }

    /**
    * User (作者): MRui
    * TODO (描述)：登出
    * Data：2018.10.04 13:10
    */
    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request,
                       HttpServletResponse response,
                       Map<String,Object> map){
        //1.从cookie里查询
        Cookie cookie = CookieUtil.get(request, Constant.COOKIE_TOKEN);
        if (cookie != null){
            //2.清除redis
            redisTemplate.opsForValue().getOperations().delete(String.format(Constant.REDIS_TOKEN_PREFIX,cookie.getValue()));
            //3.清楚cookid
            CookieUtil.set(response,Constant.COOKIE_TOKEN,null,0);
        }
        map.put("msg",ConstantEnum.LOGOUT_SUCCESS.getMessage());
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("/common/success",map);
    }
}
